﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Pong_Project.Game_objects
{
    /// <summary>
    /// A powerup that the ball can pick up
    /// </summary>
    class PowerupBall : Powerup
    {
        private Ball ball;
        private Bat effectedBat;
        private float scaleFactor;
        private Vector2 directionModifyer;


        public PowerupBall(GameObjectIdentity identity, Ball ball, Bat batAffected, Ball ballAffected, int timeToLast, int timeToLive, int minTimeBeforeSpawning, int maxTimeBeforeSpawning, float scaleFactor, Vector2 directionModifyer)
            : base(identity, ball, new Vector2(0, 0), timeToLast, timeToLive, minTimeBeforeSpawning, maxTimeBeforeSpawning)
        {
            this.ball = ball;
            this.effectedBat = null;
            this.scaleFactor = scaleFactor;
            this.directionModifyer = directionModifyer;
        }


        public override void Spawn()
        {
            base.Spawn();

            if (ball.MiddleX > Game.WindowWidth / 2)
            {
                PositionX = Game.Random.Next(100, (int)(Game.WindowWidth / 2 - Width));
            }
            else
            {
                PositionX = Game.Random.Next((int)(Game.WindowWidth / 2), (int)(Game.WindowWidth - 100 - Width));
            }
            PositionY = Game.Random.Next(10, (int)(Game.WindowHeight - 10 - Height));
        }

        public override void ApplyEffect(GameObject gameObject)
        {
            base.ApplyEffect(gameObject);

            effectedBat = ball.BatLastHit;

            if (scaleFactor != 0)
                effectedBat.ScaleY *= scaleFactor;

            if (directionModifyer != Vector2.Zero)
            {
                if (directionModifyer.X == 666 && effectedBat.Identity == GameObjectIdentity.BatLeft)
                    Score.GetInstance().ScoreLeft++;

                else if (directionModifyer.X == 666)
                    Score.GetInstance().ScoreRight++;

                else
                    effectedBat.VelocityY *= directionModifyer.Y;
            }
        }

        public override void RemoveEffect()
        {
            base.RemoveEffect();
            if (scaleFactor != 0 && effectedBat != null)
                effectedBat.ScaleY /= scaleFactor;

            if (directionModifyer != Vector2.Zero && effectedBat != null)
                effectedBat.VelocityY /= directionModifyer.Y;
        }

        public override void Reset()
        {
            base.Reset();
            if (scaleFactor != 0 && effectedBat != null)
                effectedBat.ScaleY = 1.0f;

            if (directionModifyer != Vector2.Zero && effectedBat != null)
                effectedBat.VelocityY = 1.0f;
        }
    }
}
